﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Task.Interface;
using MyLib.Service;
using Task.Entity;
using MyLib.Utils;
using MyLib.Integration; 

namespace Task
{
    public partial class TaskList : System.Web.UI.Page
    {
        ITask _task = ServiceContainer.Instance().GetService<ITask>();
        IProcessCategory _pc = ServiceContainer.Instance().GetService<IProcessCategory>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtCurrentProcessCategory.Text = Request.QueryString["CategoryName"];
                txtType.Text = Request.QueryString["Type"];
                string user = Request.QueryString["user"];
                string title = Request.QueryString["menuname"];
                if (string.IsNullOrEmpty(title))
                {
                    title = "待办任务";
                }
                litTitle.Text = title;
                if (!string.IsNullOrEmpty(user))
                {
                    txtUserAccount.Text = user;
                }
                else
                {
                    txtUserAccount.Text = SSOUtil.LoginedUser.UserAccount;
                }
                BindProcessCategory();
                BindGrid();
            }
        }       

        void BindProcessCategory()
        {
            List<ProcessCategoryEntity> lists = _pc.GetList();
            ddlProcessCategory.Items.Clear();
            ddlProcessCategory.Items.Add("");
            foreach (ProcessCategoryEntity ety in lists)
            {
                ddlProcessCategory.Items.Add(ety.CategoryName);
            }
        }

        void BindGrid()
        {
            string filter = GetFilter();
            int skipResults = (MyPager1.CurrentPageIndex - 1) * MyPager1.PageSize;
            int maxResults = MyPager1.PageSize;

            List<TaskEntity> lists = new List<TaskEntity>();
            if (txtType.Text.ToUpper().Trim() == "MYREQUEST")
            {
                lists = _task.GetMyRequest(filter, skipResults, maxResults);
                MyPager1.RecordCount = _task.GetMyRequestCount(filter);
            }
            else if (txtType.Text.ToUpper().Trim() == "MYAPPROVAL")
            {
                lists = _task.GetMyApproval(filter, skipResults, maxResults);
                MyPager1.RecordCount = _task.GetMyApprovalCount(filter);
            }
            else
            {
                lists = _task.GetMyTask(filter, skipResults, maxResults);
                MyPager1.RecordCount = _task.GetMyTaskCount(filter);
            }
            rptTask.DataSource = lists;
            rptTask.DataBind();
        }

        string GetFilter()
        {
            FilterBuilder fb = new FilterBuilder();
            txtCurrentProcessCategory.Text = ddlProcessCategory.Text;
            fb.AddLike("ProcessName", txtProcessName.Text);
            if (!string.IsNullOrEmpty(txtCurrentProcessCategory.Text))
            {
                List<ProcessEntity> processes = _pc.GetProcessList(txtCurrentProcessCategory.Text);
                List<string> strs = new List<string>();
                foreach (ProcessEntity process in processes)
                {
                    strs.Add(process.ProcessName);
                }
                fb.AddIn("ProcessName", strs.ToArray());
            }
            fb.AddEqual("ASSIGNEDTOUSER", txtUserAccount.Text);
            fb.AddScope("Incident", txtIncidentStart.Text, txtIncidentEnd.Text);
            fb.AddScope("StartTime", txtStartDate.Text, txtEndDate.Text);
            fb.AddLike("Summary", txtSummary.Text);
            return fb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        public string GetStatusImage(string subStatus)
        {
            string img = "task_active.gif";
            if (subStatus == "2") //超期
            {
                img = "task_overdue.gif";
            }

            if (subStatus == "8") //已完成
            {
                img = "task_complete.gif";
            }

            if (subStatus == "4" ) //已归档
            {
                img = "task_complete.gif";
            }

            if (subStatus == "1") //紧急
            {
                img = "task_urgent.gif";
            }

            return img;
        }

        public string GetCurrentCategoryClass(string categoryName)
        {
            if (categoryName.ToUpper().Trim() == txtCurrentProcessCategory.Text.ToUpper().Trim())
            {
                return "class='currently'";
            }
            return "";
        }

        protected void MyPager1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}